package com.java.service.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.RaceAdmin;
import com.java.mapper.RaceAdminMapper;
import com.java.service.AdminService;
import com.java.util.MyCons;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.servlet.ServletUtil;

@Service
public class AdminServiceImpl implements AdminService {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired private RaceAdminMapper adminMapper;

	@Override
	public BaseRespone login(String user, String password) {
		RaceAdmin info = adminMapper.selectByInfo(new RaceAdmin().setAdminUser(user));
		if(null == info) {
			return BaseRespone.error("管理员账号不存在");
		}
		if(!password.equals(info.getAdminPassword())) {
			return BaseRespone.error("账号密码错误");
		}
		ServletUtil.getSession().setAttribute(MyCons.ADMIN_ROLE, info);
		return BaseRespone.success("登陆成功");
	}

	@Override
	public RaceAdmin getAdmin(String adminId) {
		return adminMapper.selectByKey(adminId);
	}

	@Override
	public BaseRespone update(RaceAdmin admin) {
		RaceAdmin info = adminMapper.selectByKey(admin.getAdminId());
		info.setAdminUser(admin.getAdminUser())
		.setAdminName(admin.getAdminName());
		if(!MyTools.isEmpty(admin.getAdminPassword())) {
			info.setAdminPassword(admin.getAdminPassword());
		}
		int row = adminMapper.updateSelective(info);
		if(row == 0) {
			return BaseRespone.error("修改失败");
		}
		return BaseRespone.success("修改成功");
	}
    
    
}
